package com.hk.commons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 当天的开始时间
	 * 
	 * @return
	 */
	public static Calendar getNowTime() {
		
		Calendar calendar = Calendar.getInstance();
		 
		return calendar;
	}
	
	/**
	 * 获取之后的几天
	 * 
	 * @return
	 */
	public static Calendar getNextDays(Integer nextDays) {
		Calendar calendar = Calendar.getInstance();
		
		calendar.add(Calendar.DAY_OF_YEAR, +nextDays);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 000);
		return calendar;
	}

	/**
	 * 当天的开始时间
	 * 
	 * @return
	 */
	public static long startOfTodDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		Date date = calendar.getTime();
		return date.getTime();
	}

	/**
	 * 当天的结束时间
	 * 
	 * @return
	 */
	public static long endOfTodDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		Date date = calendar.getTime();
		return date.getTime();
	}

	/**
	 * 将 2001-01-01T12:01:01 类型转换为 Long
	 * 
	 * @param
	 *            '2001-01-01T12:01:01'
	 * @return Long
	 */
	public static long ToDateTimeFromUTCISO8601(String dateTimeString) {
		Long timeLong = null;
		String dateString = dateTimeString.substring(0, 10) + " "
				+ dateTimeString.substring(11, dateTimeString.length());

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {

			timeLong = format.parse(dateString).getTime();
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return timeLong;
	}

	/**
	 * 将 2010-01-01 类型转换为 Long
	 * 
	 * @param dayString
	 *            '2001-01-01'
	 * @return Long
	 */
	public static long ToDateTimeFromDay(String dayString) {
		Long timeLong = Long.valueOf(0);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {

			timeLong = format.parse(dayString).getTime();
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return timeLong;
	}

	/**
	 * 将 2010-01-01 类型转换为 String
	 * 
	 * @param
	 *            '2001-01-01 01:01:01'
	 * @return Long
	 */
	public static String longToLongDate(long lo) {
		Date date = new Date(lo);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sd.format(date);
	}
	
	/**
	 * 获取当前日期 '2001年01月01日 '
	 *
	 * @return Long
	 */
	public static String getCurTimeStr() {
		Long lo = System.currentTimeMillis();
		Date date = new Date(lo);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy年 MM月 dd日 ");
		return sd.format(date);
	}

	/**
	 * 获取当前日期 '2001年01月01日 '
	 *
	 * @param lo time
	 *            '2001年01月01日 '
	 * @return Long
	 */
	public static String getCurTimeStr(Long lo) {
		Date date = new Date(lo);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy年 MM月 dd日 ");
		return sd.format(date);
	}

	/**
	 * 将 1464056763499 类型转换为 String
	 * 
	 * @param
	 *            '2001-01-01'
	 * @return Long
	 */
	public static String longToShortDate(long lo) {
		Date date = new Date(lo);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		return sd.format(date);
	}

	public static String longToShortDateZW(long lo) {
		Date date = new Date(lo);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日");
		return sd.format(date);
	}

	/**
	 * 
	 * 把毫秒转化成日期
	 * 
	 * @param dateFormat
	 *            (日期格式，例如：yyyyMMddHHmmss)
	 * 
	 * @param millSec
	 *            (毫秒数)
	 * 
	 * @return
	 */

	@SuppressWarnings("unused")
	public static String transferLongToDate(String dateFormat, Long millSec) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date = new Date(millSec);
		return sdf.format(date);
	}

	/**
	 * 
	 * 把毫秒转化成日期
	 * 
	 * @param (例如：yyyyMMddHHmmssMMM)
	 * 
	 * 
	 * 
	 * @return 2016-07-05 11:10:24
	 */
	public static Long transferStringToLong(String dayString) {
		Long timeLong = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		
		if(dayString.length() == 8){
		  format = new SimpleDateFormat("yyyyMMdd");
		}
		
		try {
			timeLong = format.parse(dayString).getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return timeLong;
	}

	/**
	 * Date转yyyy-MM-dd格式
	 * Sat Sep 19 17:42:39 CST 2015 转 2015-09-19格式
	 * @param date Date格式时间
	 * @return "yyyy-MM-dd"格式字符串
	 */
	public static String DateToFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//2016-01-01
		return sdf.format(date);
	}

}
